<template>
	<div class="addProject business-workAdd">
		<div class="addForm zll-form">
			<div v-if="!isShenBao">
				<div class="Search_Top_Input">
					<div class="search_list">
						<div class="input_flex daterange">
							<el-date-picker v-model="queryForm.dutyDay" type="date" class="input_right"
											format="yyyy-MM-dd"
											value-format="yyyy-MM-dd"
											placeholder="请选择日期">
							</el-date-picker>
						</div>
						<div class="input_flex">
							<el-select clearable v-model="queryForm.itemId" placeholder="品类">
								<el-option v-for="(item,index) in itemList" :key="index" :label="item"
										   :value="item"/>
							</el-select>
						</div>
					</div>
					<div class="search_bt">
						<span class="zll-search" @click="getOnDutyBgTaskListByItemName">搜索</span>
						<span class="zll-search-reset" @click="searchReset()">重置</span>
					</div>
					<p class="zll-botton reject" @click="goShenBao()">扣减申报</p>
				</div>
				<div class="tableList" style="margin-top: 0 !important;">
					<table border="1" v-loading="tableLoading">
						<tr>
							<th></th>
							<th>品类</th>
							<th>机台</th>
							<th>任务名称</th>
							<th width="200">规格</th>
							<th>单件万支</th>
							<th>件数</th>
							<th>产能万支</th>
							<th>工序</th>
							<th>姓名</th>
							<th>考勤</th>
							<th>备注</th>
						</tr>
						<tr class="add_Table" v-for="(item,index) in bgTasks" :key="index">
							<td width="40">
								<el-checkbox v-model="item.checked" @change="changeCheck(item)"></el-checkbox>
							</td>
							<td>
								<span>{{item.itemRefName}}</span>
							</td>
							<td>
								<span>{{item.deviceCode}}</span>
							</td>
							<td>
								<span>{{item.planName}}</span>
							</td>
							<td>
								<span>{{item.gradeValue}}</span>
							</td>
							<td>
								<span>{{item.thNumber}}</span>
							</td>
							<td>
								<span>{{item.pieceNum}}</span>
							</td>
							<td>
								<span>{{item.totalNum}}</span>
							</td>
							<td>
								<p v-for="(item2, index2) in item.onDutyBgTaskUsers" :key="index2">
									<span>{{item2.gongxu}}</span>
								</p>
							</td>
							<td width="150">
								<p v-for="(item2, index2) in item.onDutyBgTaskUsers" :key="index2">
									<span>{{item2.userName}}</span>
								</p>
							</td>
							<td width="150">
								<p v-for="(item2, index2) in item.onDutyBgTaskUsers" :key="index2">
									<span>{{item2.kaoQin}}</span>
								</p>
							</td>

							<td>
								<span>{{item.remark}}</span>
							</td>
						</tr>
					</table>
				</div>
			</div>
			<div v-else>
				<div class="top_part">
					<p>{{selectBgTask.dutyDay}}，{{selectBgTask.whiteDay =='1'?'白班':'晚班'}} / {{selectBgTask.itemRefName}}
						/ {{selectBgTask.deviceCode}} / {{selectBgTask.planName}} / {{selectBgTask.gradeValue}}</p>
					<div class="_right">
						<div class="text-formList">
							<span class="text_left">申报人：</span>
							<span class="text_right">{{addForm.applyUser}}</span>
						</div>
						<div class="text-formList">
							<span class="text_left">状态：</span>
							<span class="text_right">{{addForm.deduceStatusCn}}</span>
						</div>
					</div>
				</div>
				<div class="tableList">
					<table border="1">
						<tr>
							<th>单件万支</th>
							<th>件数</th>
							<th>产能万支</th>
							<th>工序</th>
							<th>姓名</th>
							<th>考勤</th>
							<th>备注</th>
							<th>扣减件数</th>
						</tr>
						<tr class="add_Table">
							<td><p>{{ selectBgTask.thNumber }}</p></td>
							<td><p>{{ selectBgTask.pieceNum }}</p></td>
							<td><p>{{ selectBgTask.totalNum }}</p></td>
							<td>
								<p v-for="(item2, index2) in selectBgTask.onDutyBgTaskUsers" :key="index2">
									<span>{{item2.gongxu}}</span>
								</p>
							</td>
							<td width="150">
								<p v-for="(item2, index2) in selectBgTask.onDutyBgTaskUsers" :key="index2">
									<span>{{item2.userName}}</span>
								</p>
							</td>
							<td width="150">
								<p v-for="(item2, index2) in selectBgTask.onDutyBgTaskUsers" :key="index2">
									<span>{{item2.kaoQin}}</span>
								</p>
							</td>
							<td><p>{{ selectBgTask.remark }}</p></td>
							<td>
								<el-input
									@input="selectBgTask.deleteCount=selectBgTask.deleteCount.replace(/[^\d]/g,'')"
									v-model="selectBgTask.deleteCount" placeholder="" class="text_center" clearable
									:disabled="type == 'see'"></el-input>
							</td>
						</tr>
					</table>
				</div>
			</div>
		</div>
	</div>
</template>
<script>
	import {getStore} from "@/plugins/localStore";
	export default {
		props: ['type', 'editData'],
		data() {
			return {
				queryForm: {
					dutyDay: '',
					itemId: '',
				},
				itemList: ['单支连排', '多支', '伸缩', 'U型'],
				addForm: {id:'',bgId:''},
				isShenBao: false,
				selectBgTask: {deleteCount: ''},
				bgTasks: [],
				tableLoading: false,

				count: 0,
				user:{},
			}
		},

		methods: {

			changeCheck(item) {
				this.bgTasks.forEach((data) => {
					if (data.id != item.id) {
						data.checked = false;
					}
				})
			},
			getOnDutyBgTaskListByItemName() {
				let self = this;
				if (!self.queryForm.dutyDay) {
					self.$message.warning('请选择时间');
					return;
				}
				if (!self.queryForm.itemId) {
					self.$message.warning('品类');
					return;
				}
				self.bgTasks = [];
				self.tableLoading = true;
				self.$http({
					url: "/jinl/bgTask/getOnDutyBgTaskListByItemName",
					method: "post",
					params: self.queryForm
				}).then(resp => {
					self.tableLoading = false;
					if (resp.success) {
						for (let data of resp.result) {
							self.$set(data, 'checked', false);
							let pieceNum = data.pieceNum;
							let thNumber = data.thNumber;
							let totalNum = parseFloat(pieceNum) * parseFloat(thNumber);
							data.totalNum = totalNum.toFixed(2);
						}
						self.bgTasks = resp.result;
						console.log(self.bgTasks)
					}
				});
			},

			getOnDutyBgTaskWithUserListById() {
				let self = this;
				self.$http({
					url: "/jinl/bgTask/getOnDutyBgTaskWithUserListById",
					method: "post",
					params: {id:self.addForm.bgId}
				}).then(resp => {
					self.tableLoading = false;
					if (resp.success) {
						self.selectBgTask= resp.result
						self.selectBgTask.deleteCount = self.addForm.deleteCount;
						let pieceNum = self.selectBgTask.pieceNum;
						let thNumber = self.selectBgTask.thNumber;
						let totalNum = parseFloat(pieceNum) * parseFloat(thNumber);
						self.selectBgTask.totalNum = totalNum.toFixed(2);
					}
				});
			},


			searchReset() {
				this.queryForm = {
					dutyDay: '',
					itemId: ''
				};
				this.bgTasks = [];
			},
			goShenBao() {
				let self = this;
				let selectObj = self.bgTasks.find(val => {
					return val.checked
				});
				if (!selectObj || !selectObj.id) {
					self.$message.warning('请选择一条数据');
					return;
				}
				self.selectBgTask = Object.assign({}, selectObj);
				self.isShenBao = true
			},
			submitForm() {
				let self = this;
				if (!self.selectBgTask.deleteCount) {
					self.$message.warning('请填写扣减件数');
					return;
				}
				if (self.count == 0) {
					++self.count;
					let userNames = '';
					for(let i = 0; i < self.selectBgTask.onDutyBgTaskUsers.length;i++){
						userNames += self.selectBgTask.onDutyBgTaskUsers[i].userName;
						if(i != self.selectBgTask.onDutyBgTaskUsers.length -1){
							userNames+=',';
						}
					}
					let params = {id:self.addForm.id,applyUser:self.user.userName,bgId:self.selectBgTask.id,userNames:userNames,deleteCount:self.selectBgTask.deleteCount};
					self.$http({
						url: "/jinl/reduce/updateOnDutyReduce",
						method: "post",
						params: params
					}).then(resp => {
						self.count = 0;
						if (resp.success) {
							self.$eventBus.$emit('updateOndutyReduceList');
							return;
						}
						self.$message.error(resp.message)
					});
				}
			},
		},
		mounted() {

		},
		watch:{
			editData(val) {
				this.addForm = Object.assign({},val);
				console.log(val)
				if(this.type == 'see'){
					this.getOnDutyBgTaskWithUserListById();
					this.isShenBao = true
				}else{
					this.user = getStore();
					this.addForm.applyUser = this.user.userName;
					this.addForm.deduceStatusCn = '待扣减';
				}

			},
		},
		components: {}
	}
</script>
<style lang="scss" scoped>
	@import "@/style/dialog.scss";

	.business-workAdd.addProject {
		.top_part {
			display: flex;
			justify-content: space-between;

			p {
				line-height: 32px;
			}

			._right {
				display: flex;

				.text-formList {
					margin-left: 15px;
					width: 140px;

					// .text_right {
					//     color: #2196f3;
					// }
				}
			}
		}

		.Search_Top_Input {
			.search_list {
				display: flex;
			}

			.zll-botton {
				height: 26px;
				line-height: 26px;
				padding: 0 15px;
				margin: 0;
			}
		}

		.formList {
			height: 32px;
		}

		.tableList {
			margin-bottom: 20px;

			table {
				tr {
					background: #fff !important;

					td {
						padding: 1px 3px;
					}
				}
			}
		}
	}
</style>